#!/usr/bin/python
# -*- coding: utf-8 -*-
# This script is an interface of database operation.

import shelve
databasefile = "doubanDB"

#BASIC
# douban id
def get_douban_id():
  dbcon = shelve.open(databasefile)
  if dbcon.has_key("userid"):
    return dbcon["userid"]
  else:
    return 0

def set_douban_id(id):
  dbcon = shelve.open(databasefile)
  dbcon["userid"] =  id
  dbcon.close()

# save path
def get_save_path():
  dbcon = shelve.open(databasefile)
  if dbcon.has_key("savePath"):
    return dbcon["savePath"]
  else:
    return ""

def set_save_path(path):
  dbcon = shelve.open(databasefile)
  dbcon["savePath"] = path
  dbcon.close()

# music info
# @dic {0:{title:{"album":album, "album_url":album_url, "artist":artist}, title2:{}}, 1:{}}
def set_music_info(dic):
  dbcon = shelve.open(databasefile)
  dbcon["musicinfo"] = dic
  dbcon.close()

def get_music_info():
  dbcon = shelve.open(databasefile)
  if dbcon.has_key("musicinfo"):
    return dbcon["musicinfo"]
  else:
    return {0:{},1:{}}

#END BASIC

#BEGIN ADVANCED

def print_music_info():
  info = get_music_info()
  downloaded_dic = info[1]
  undownloaded_dic = info[0]
  print ">>> downloaded:"
  for key in downloaded_dic.keys():
    music_entry = downloaded_dic[key]
    print " | ".join([key, music_entry["album"], music_entry["artist"]])
  print "\n\n>>> undownloaded:"
  for key in undownloaded_dic.keys():
    music_entry = undownloaded_dic[key]
    if music_entry.has_key("artist"):
      print " | ".join([key, music_entry["album"], music_entry["artist"]])
    else:
      print " | ".join([key, music_entry["album"], music_entry["album_url"]])

def if_found_music(title):
  """This method tells if music has already been downloaded"""
  info = get_music_info()
  if info[1].has_key(title):
    return "found"
  else:
    return "unfound"

def ever_search_music(title):
  """This method tells if music has already in database"""
  info = get_music_info()
  if info[0].has_key(title):
    return "found"
  else:
    return if_found_music(title)

def insert_found_music(music_entry):
  """This method mark music downloaded
   delete music_entry in info[0] if exists
   add music_entry into info[1]"""
  info = get_music_info()
  if info[0].has_key(music_entry.keys()[0]):
    del info[0][music_entry.keys()[0]]
  info[1][music_entry.keys()[0]] = music_entry.values()[0]
  set_music_info(info)

def insert_unfound_music(music_entry):
  """This method mark music unfound"""
  if if_found_music(music_entry.keys()[0]) == "unfound":
    info = get_music_info()
    info[0][music_entry.keys()[0]] = music_entry.values()[0]
    set_music_info(info)
#END ADVANCED

if __name__ == "__main__":

  #info = get_music_info()

  #for key in info[0].keys():
  #  info[0][key.strip()] = info[0][key]
  #  del info[0][key]
  #for key in info[1].keys():
  #  info[1][key.strip()] = info[1][key]
  #  del info[1][key]
  #set_music_info(info)
  print_music_info()

  #del info[1]["星期一天气晴我离开你"]
  #del info[1]["她在睡梦中"]
  #del info[1]["Scarborough Fair"]
  #set_music_info(info)
